package yd.launcher.service;

import yd.launcher.R;

import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义配置存储方式
 * 不使用Object存储，因为当Action增加属性时，旧的配置文件无法被读取
 */
public class DataService {
    private int[] bounds;
    private List<Panel> panels;

    public DataService(Path path){
        readFromFile(path);
    }

    public int[] getBounds() {
        return bounds;
    }

    public List<Panel> getPanels() {
        return panels;
    }

    public void setBounds(int width, int height, int x, int y) {
        bounds = new int[]{width, height, x, y};
    }

    public void readFromFile(Path configFilePath) {
        if (Files.exists(configFilePath)) {
            List<String> lines = null;
            try {
                lines = Files.readAllLines(configFilePath, StandardCharsets.UTF_8);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
            Map<String, Object> props = new LinkedHashMap<>();
            for (String s : lines) {
                if (s.contains("=")) {
                    int i = s.indexOf("=");
                    props.put(s.substring(0, i), s.substring(i + 1));
                }
            }
            String bs = (String)props.get("bounds");
            if (bs != null) {
                String[] bss = bs.split(",");
                bounds = new int[]{Integer.valueOf(bss[0]), Integer.valueOf(bss[1]), Integer.valueOf(bss[2]), Integer.valueOf(bss[3])};
            }
            panels = Panel.fromProperties(props);
        }else{
            panels = new ArrayList<>();
        }
    }

    public void saveToFile(Path configFilePath) {
        if(!Files.exists(configFilePath.getParent())){
            try {
                Files.createDirectories(configFilePath.getParent());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Path bak3 = configFilePath.getParent().resolve(configFilePath.getFileName() + ".bak3");
        Path bak2 = configFilePath.getParent().resolve(configFilePath.getFileName() + ".bak2");
        Path bak1 = configFilePath.getParent().resolve(configFilePath.getFileName() + ".bak1");

        if (Files.exists(bak3)) {
            bak3.toFile().delete();
        }
        if (Files.exists(bak2)) {
            bak2.toFile().renameTo(bak3.toFile());
        }
        if (Files.exists(bak1)) {
            bak1.toFile().renameTo(bak2.toFile());
        }
        if (Files.exists(configFilePath)) {
            configFilePath.toFile().renameTo(bak1.toFile());
        }

        Map<String, Object> props = Panel.toProperties(panels);
        List<String> properties=new ArrayList<>(props.size());
        properties.add(String.format("bounds=%d,%d,%d,%d",bounds[0],bounds[1],bounds[2],bounds[3]));
        for (Map.Entry entry : props.entrySet()) {
            properties.add(entry.getKey() + "=" + entry.getValue());
        }
        try {
            Files.write(configFilePath, properties, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
